﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Poco;

namespace Metier
{
    public class GestionPlat
    {
        public void Ajouter(Plat plat)
        {
            try
            {
                using (Context db = new Context())
                {
                    db.Plats.AddObject(plat);
                    db.SaveChanges();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public List<Plat> Lister()
        {
            try
            {
                using (Context db = new Context())
                {
                    return db.Plats.ToList();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public List<Plat> Lister(Specialite specialitePlat)
        {
            try
            {
                using (Context db = new Context())
                {
                    return (from p in db.Plats
                            where p.Specialite.Id == specialitePlat.Id
                            select p).ToList();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public List<Plat> Lister(string typePlat)
        {
            try
            {
                using (Context db = new Context())
                {
                    return (from n in db.Plats
                                where n.Type == typePlat
                                select n).ToList();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public Plat Chercher(int platId)
        {
            try
            {
                using (Context db = new Context())
                {
                    return db.Plats.Where(p => p.Id == platId).FirstOrDefault();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public Plat Chercher(string nom)
        {
            try
            {
                using (Context db = new Context())
                {
                    return (from p in db.Plats
                            where p.Nom == nom
                            select p).FirstOrDefault();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void MettreAJour(Plat plat)
        {
            try
            {
                using (Context db = new Context())
                {
                    db.Plats.Attach(plat);
                    db.ObjectStateManager.ChangeObjectState(plat, System.Data.EntityState.Modified);
                    db.SaveChanges();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void Supprimer(Plat plat)
        {
            try
            {
                using (Context db = new Context())
                {
                    db.Plats.DeleteObject(plat);
                    db.SaveChanges();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

    }
}
